﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyMainSite2.Data.Repositories
{
	public class AdminRoleRepository : IAdminRoleRepository
	{
		#region Public Methods

		virtual public bool IsUserInRole(string userName, string role)
		{
			if (String.IsNullOrWhiteSpace(userName))
				throw new ArgumentException("userName");

			if (String.IsNullOrWhiteSpace(role))
				throw new ArgumentException("role");

			var context = new MyMainSite2Entities();

			var isUserInRole = (from u in context.tblAdminUsers
								where u.tblAdminRoles.Select(r => r.Name == role).Count() > 0
								select u).Count() > 0;

			return isUserInRole;
		}

		virtual public List<tblAdminRole> GetRolesForUser(string userName)
		{
			if (String.IsNullOrWhiteSpace(userName))
				throw new ArgumentException("userName");

			var context = new MyMainSite2Entities();

			var user = (from u in context.tblAdminUsers
					    where u.UserName == userName
					    select u).SingleOrDefault();

			List<tblAdminRole> roles = null;

			if (user != null)
				roles = user.tblAdminRoles.ToList();

			return roles;
		}

		#endregion
	}
}
